import { BattleStatusParams as Params } from '@swda/configs/params'

let SWDA_BattleStatusWindow = {};

//=============================================================================
// BattleManager
//=============================================================================

SWDA_BattleStatusWindow.BattleManager_startInput = BattleManager.startInput;
BattleManager.startInput = function () {
  SWDA_BattleStatusWindow.BattleManager_startInput.call(this);
  this.refreshStatus();
};

//=============================================================================
// Game_Battler
//=============================================================================

SWDA_BattleStatusWindow.Game_Action_clear = Game_Action.prototype.clear;
Game_Action.prototype.clear = function () {
  SWDA_BattleStatusWindow.Game_Action_clear.call(this);
  this.subject().refresh();
};

SWDA_BattleStatusWindow.Game_Action_setSkill = Game_Action.prototype.setSkill;
Game_Action.prototype.setSkill = function (skillId) {
  SWDA_BattleStatusWindow.Game_Action_setSkill.call(this, skillId);
  this.subject().refresh();
};

SWDA_BattleStatusWindow.Game_Action_setItem = Game_Action.prototype.setItem;
Game_Action.prototype.setItem = function (itemId) {
  SWDA_BattleStatusWindow.Game_Action_setItem.call(this, itemId);
  this.subject().refresh();
};

SWDA_BattleStatusWindow.Game_Action_setItemObject = Game_Action.prototype.setItemObject;
Game_Action.prototype.setItemObject = function (object) {
  SWDA_BattleStatusWindow.Game_Action_setItemObject.call(this, object);
  this.subject().refresh();
};

//=============================================================================
// Game_Actor
//=============================================================================

SWDA_BattleStatusWindow.Game_Actor_isSpriteVisible = Game_Actor.prototype.isSpriteVisible;
Game_Actor.prototype.isSpriteVisible = function () {
  if (Params.BSWShowAni && !$gameSystem.isSideView()) {
    return true;
  }
  return SWDA_BattleStatusWindow.Game_Actor_isSpriteVisible.call(this);
};

SWDA_BattleStatusWindow.Game_Actor_changeClass = Game_Actor.prototype.changeClass;
Game_Actor.prototype.changeClass = function (classId, keepExp) {
  SWDA_BattleStatusWindow.Game_Actor_changeClass.call(this, classId, keepExp);
  this.battleStatusWindowRefresh();
};

SWDA_BattleStatusWindow.Game_Actor_setCharacterImage =
  Game_Actor.prototype.setCharacterImage;
Game_Actor.prototype.setCharacterImage = function (name, index) {
  SWDA_BattleStatusWindow.Game_Actor_setCharacterImage.call(this, name, index);
  this.battleStatusWindowRefresh();
};

SWDA_BattleStatusWindow.Game_Actor_setFaceImage =
  Game_Actor.prototype.setFaceImage;
Game_Actor.prototype.setFaceImage = function (faceName, faceIndex) {
  SWDA_BattleStatusWindow.Game_Actor_setFaceImage.call(this, faceName, faceIndex);
  this.battleStatusWindowRefresh();
};

SWDA_BattleStatusWindow.Game_Actor_setBattlerImage =
  Game_Actor.prototype.setBattlerImage;
Game_Actor.prototype.setBattlerImage = function (battlerName) {
  SWDA_BattleStatusWindow.Game_Actor_setBattlerImage.call(this, battlerName);
  this.battleStatusWindowRefresh();
};

Game_Actor.prototype.battleStatusWindowRefresh = function () {
  if (!$gameParty.inBattle()) return;
  if (!$gameParty.battleMembers().contains(this)) return; // 不显示宠物
  //if (!$gameParty.battleMembersNotPets().contains(this)) return;
  BattleManager.refreshStatus();
};




//=============================================================================
// Sprite_Actor【我方战斗图】
//=============================================================================

SWDA_BattleStatusWindow.Sprite_Actor_createMainSprite =
  Sprite_Actor.prototype.createMainSprite;
Sprite_Actor.prototype.createMainSprite = function () {
  SWDA_BattleStatusWindow.Sprite_Actor_createMainSprite.call(this);
  if ($gameSystem.isSideView()) return;
  if (Params.BSWShowSprite) {
    this._effectTarget = this._mainSprite || this;
  } else {
    this._effectTarget = this;
  }
};

SWDA_BattleStatusWindow.Sprite_Actor_setActorHome = Sprite_Actor.prototype.setActorHome;
Sprite_Actor.prototype.setActorHome = function (index) {
  if (Params.BSWAlignAni && !$gameSystem.isSideView()) {
    this.setActorHomeFrontView(index);
  } else {
    SWDA_BattleStatusWindow.Sprite_Actor_setActorHome.call(this, index);
  }
};

Sprite_Actor.prototype.setActorHomeFrontView = function (index) {

  let statusHeight = 4;

  statusHeight *= Window_Base.prototype.lineHeight.call(this);
  statusHeight += Window_Base.prototype.standardPadding.call(this) * 2;
  let screenW = Graphics.boxWidth;
  //let windowW = Window_PartyCommand.prototype.windowWidth.call(this);
  //screenW -= windowW;
  //windowW /= 2;
  if (Params.BSWAdjustCol) {
    //let size = $gameParty.battleMembers().length;//不显示宠物
    let size = $gameParty.battleMembersNotPets().length;
  } else {
    let size = $gameParty.maxBattleMembers();
  }

  let homeX = screenW / size * index + screenW / (size * 2);
  homeX += Params.BSWXOffset;
  //let homeY = Graphics.boxHeight - statusHeight;
  //homeY += Params.BSWYOffset;
  let homeY = Graphics.boxHeight;	//SWDA：直接显示在最底部
  this.setHome(homeX, homeY);
  this.moveToStartPosition();
};

SWDA_BattleStatusWindow.Sprite_Actor_update = Sprite_Actor.prototype.update;
Sprite_Actor.prototype.update = function () {
  SWDA_BattleStatusWindow.Sprite_Actor_update.call(this);
  if (!this._actor) return;
  if ($gameSystem.isSideView()) return;
  if (Params.BSWShowSprite) return;
  this.hideAllSideviewSprites();
};

Sprite_Actor.prototype.hideAllSideviewSprites = function () {
  this._mainSprite.visible = false;
  this._shadowSprite.visible = false;
  this._weaponSprite.visible = false;
  this._stateSprite.visible = false;
};

// 伤害弹出位置
Sprite_Actor.prototype.damageOffsetX = function () {
  return Params.BSWDamageXOffset;
};

Sprite_Actor.prototype.damageOffsetY = function () {
  return (ConfigManager.damageStyle ? 0 : -100) + Params.BSWDamageYOffset;
};

Sprite_Enemy.prototype.damageOffsetX = function () {
  return Params.BSWEnemyDamageXOffset;
};

Sprite_Enemy.prototype.damageOffsetY = function () {
  return Params.BSWEnemyDamageYOffset;
};

//=============================================================================
// Window_Base【Aciton图标】【ATB槽】【重要】
//=============================================================================

Window_Base.prototype.drawActorActionIcon = function (actor, wx, wy) {
  let icon = Params.NoAction;
  if (actor.currentAction() && actor.currentAction().item()) {
    icon = actor.currentAction().item().iconIndex || Params.NoAction;
  }
  this.drawIcon(icon, wx, wy);
};

// 画ATB槽（为避免和YEP_X_VisualATBGauge冲突，后面补了个SWD）
Window_Base.prototype.drawActorAtbGaugeSWD = function (actor, wx, wy, ww = 96) {
  if (!actor) return;
  let color1 = this.atbGaugeColor1(actor);
  let color2 = this.atbGaugeColor2(actor);

  let rate = 0
  if (actor.atbRate() < 1) {
    rate = actor.atbRate();
  } else if (actor.atbRate() >= 1 && actor.atbChargeRate() >= 0) {
    rate = 1;
  } else {
    rate = 0;
  }

  this.drawATBGauge(wx, wy, ww, rate, color1, color2);
  //if (actor.atbChargeRate() > 0) this.drawAtbChargeGauge(actor, wx, wy, ww);	//取消充能槽
  if (this.atbGaugeStyle() === 2) {
    let text = Yanfly.Param.ATBGaugeText;
    let align = Yanfly.Param.ATBGaugeAlign;
    if (actor.atbRate() >= 1) {
      this.changeTextColor(this.normalColor());
    } else {
      this.changeTextColor(this.systemColor());
    }
    this.drawText(text, wx, wy, ww, align);
    this.resetTextColor();
  }
};

Window_Base.prototype.drawATBGauge = function (x, y, width, rate, color1, color2) {
  let fillW = Math.floor(width * rate);
  let gaugeY = y;
  this._atbContents.bitmap.fillRect(x, gaugeY, width, 8, this.gaugeBackColor());
  this._atbContents.bitmap.gradientFillRect(x + 1, gaugeY + 1, fillW - 2, 6, color1, color2);
};
/*
Window_Base.prototype.drawAtbChargeGauge = function(actor, wx, wy, ww) {
    let color1 = this.textColor(Yanfly.Param.ATBColorChar1);
    let color2 = this.textColor(Yanfly.Param.ATBColorChar2);
    let rate = actor.atbChargeRate();
    this.drawGauge(wx, wy, ww * rate, 1, color1, color2);
};*/
//=============================================================================
// Window_PartyCommand【队伍指令】？——跳过回合
//=============================================================================

Window_PartyCommand.prototype.processHandling = function () {
  if (this.isOpenAndActive() && Params.BSWPageUpDn) {
    if (this.isHandled('pagedown') && Input.isRepeated('pagedown')) {
      return this.processPagedown();
    }
  }
  Window_Selectable.prototype.processHandling.call(this);
  if (this.isOpenAndActive() && Params.BSWLfRt) {
    if (this.isHandled('right') && Input.isRepeated('right')) {
      this.processRight();
    }
  }
};

Window_PartyCommand.prototype.processRight = function () {
  SoundManager.playCursor();
  this.updateInputData();
  this.deactivate();
  this.callHandler('right');
};

//=============================================================================
// Window_ActorCommand【角色指令】——跳过回合，切换角色
//=============================================================================

Window_ActorCommand.prototype.processHandling = function () {
  if (this.isOpenAndActive() && Params.BSWPageUpDn) {
    if (this.isHandled('pageup') && Input.isRepeated('pageup')) {
      return this.processPageup();
    } else if (this.isHandled('pagedown') && Input.isRepeated('pagedown')) {
      return this.processPagedown();
    }
  }
  Window_Selectable.prototype.processHandling.call(this);
  if (this.isOpenAndActive() && Params.BSWLfRt) {
    if (this.isHandled('left') && Input.isRepeated('left')) {
      this.processLeft();
    } else if (this.isHandled('right') && Input.isRepeated('right')) {
      this.processRight();
    }
  }
};

Window_ActorCommand.prototype.processLeft = function () {
  SoundManager.playCursor();
  this.updateInputData();
  this.deactivate();
  this.callHandler('left');
};

Window_ActorCommand.prototype.processRight = function () {
  if (SceneManager._scene.isAllowRightCommand()) {
    SoundManager.playCursor();
  }
  this.updateInputData();
  this.deactivate();
  this.callHandler('right');
};

Window_ActorCommand.prototype.processCancel = function () {
  let action = BattleManager.inputtingAction();
  if (action) action.clear();
  Window_Command.prototype.processCancel.call(this);
};

//=============================================================================
// Window_PartyCommand【命令窗口】【重要】战斗/逃跑窗口
//=============================================================================
Window_PartyCommand.prototype.initialize = function () {
  let y = Graphics.boxHeight - this.windowHeight() * 2;
  Window_Command.prototype.initialize.call(this, 0, y);
  this.openness = 0;
  this.deactivate();
};
//=============================================================================
// Window_ActorCommand【命令窗口】【重要】
//=============================================================================

Window_ActorCommand.prototype.initialize = function () {
  let y = Graphics.boxHeight - this.windowHeight() * 2;
  Window_Command.prototype.initialize.call(this, 0, y);
  this.openness = 0;
  this.deactivate();
  this._actor = null;
};
//=============================================================================
// Window_BattleStatus【战斗状态】【重要】
//=============================================================================

Window_BattleStatus.prototype.initialize = function () {
  let size = $gameParty.maxBattleMembers();

  if (Params.BSWAdjustCol) {
    //let size = $gameParty.battleMembers().length;//不显示宠物
    size = $gameParty.battleMembersNotPets().length;
  } else {
    size = $gameParty.maxBattleMembers();
  }

  let width = this.windowWidth();
  let height = this.windowHeight();
  // let x = (Graphics.boxWidth - width) / 2 ;
  let x = Graphics.boxWidth / size / 2 - 160 - this.standardPadding();
  let y = Graphics.boxHeight - height + this.standardPadding();

  Window_Selectable.prototype.initialize.call(this, x, y, width, height);
  this.refresh();
  this.openness = 0;
};

Window_BattleStatus.prototype.windowWidth = function () {
  return Graphics.boxWidth + this.standardPadding() * 2;	//修改状态栏位置
};

Window_BattleStatus.prototype.maxItems = function () {
  return $gameParty.battleMembersNotPets().length;
};

// 不懂为啥，战斗中，目标选择实际是enemy窗口控制。
Window_BattleEnemy.prototype.isHorizontal = function() {
  return true;
};

// 坐标刷新（我方队员数量变化（如单挑）时刷新
Window_BattleStatus.prototype.positionRefresh = function (index) {
  let x = Graphics.boxWidth / this.maxItems() / 2 - 160 - this.standardPadding();
  this.x = x;
  this.refresh();

  // 一并更新角色图坐标
  for (let i = 0; i < $gameParty.maxBattleMembers(); i++) {
    BattleManager._spriteset._actorSprites[i].setActorHome(i);
    BattleManager._spriteset._actorSprites[i].startEntryMotion();
  }

};


Window_BattleStatus.prototype.createContents = function () {
  this.createATBContents();
  this.createFaceContents();
  this.createCursorContents();
  this._currentMax = Params.BSWCurrentMax;
  Window_Selectable.prototype.createContents.call(this);
  this.opacity = 0;
  //this.cursorOpacity = 0;
  this._windowCursorSprite.alpha = 0;
};

// 创建ATBContents
Window_BattleStatus.prototype.createATBContents = function () {
  this._atbContents = new Sprite();
  let ww = this.contentsWidth();
  let wy = this.contentsHeight();
  this._atbContents.bitmap = new Bitmap(ww, wy);
  this.addChildAt(this._atbContents, 1);
  this._atbContents.move(this.standardPadding(), this.standardPadding());
};
// 创建脸图Contents
Window_BattleStatus.prototype.createFaceContents = function () {
  this._faceContents = new Sprite();
  let ww = this.contentsWidth();
  let wy = this.contentsHeight();
  this._faceContents.bitmap = new Bitmap(ww, wy);
  this.addChildAt(this._faceContents, 2);
  this._faceContents.move(this.standardPadding(), this.standardPadding());
};
// 创建光标Contents
Window_BattleStatus.prototype.createCursorContents = function () {
  this._cursorContents = new Sprite();
  let ww = this.contentsWidth();
  let wy = this.contentsHeight();
  this._cursorContents.bitmap = new Bitmap(ww, wy);
  this.addChildAt(this._cursorContents, 3);
  this._cursorContents.move(this.standardPadding(), this.standardPadding());
};

Window_BattleStatus.prototype.drawAllItems = function () {
  Window_Selectable.prototype.drawAllItems.call(this);
  this.drawAllFaces();
  this.drawAllCursors();
  this.drawAllATBGauges();
};
// 画所有人的脸
Window_BattleStatus.prototype.drawAllFaces = function () {
  //for (let i = 0; i < $gameParty.battleMembers().length; ++i) {//不显示宠物
  for (let i = 0; i < $gameParty.battleMembersNotPets().length; ++i) {
    //let member = $gameParty.battleMembers()[i];//不显示宠物
    let member = $gameParty.battleMembersNotPets()[i];
    let bitmap = ImageManager.loadFace(member.faceName());
    if (bitmap.width <= 0) return setTimeout(this.drawAllFaces.bind(this), 5);
  }

  this._faceContents.bitmap.clear();

  for (let i = 0; i < this.maxItems(); ++i) {
    this.drawStatusFace(i);
  }
};

// 画光标
Window_BattleStatus.prototype.drawAllCursors = function () {
  this._cursorContents.bitmap.clear();
  for (let i = 0; i < this.maxItems(); ++i) {
    this.drawCursor(i);
  }
};

Window_BattleStatus.prototype.drawAllATBGauges = function () {
  for (let i = 0; i < this.maxItems(); ++i) {
    //let actor = $gameParty.battleMembers()[i];//不显示宠物
    let actor = $gameParty.battleMembersNotPets()[i];
    //this.drawATBArea(this.atbAreaRect(i), actor);
  }
};
//刷新状态
Window_BattleStatus.prototype.updateStatusRequests = function () {
  if (BattleManager._victoryPhase) return;
  //for (let i = 0; i < $gameParty.battleMembers().length; ++i) {//不显示宠物
  for (let i = 0; i < $gameParty.battleMembersNotPets().length; ++i) {
    //let actor = $gameParty.battleMembers()[i];//不显示宠物
    let actor = $gameParty.battleMembersNotPets()[i];
    if (!actor) continue;
    if (actor.isStatusRefreshRequested()) this.processStatusRefresh(i);
  }
};

Window_BattleStatus.prototype.processStatusRefresh = function (index) {
  //let actor = $gameParty.battleMembers()[index];//不显示宠物
  let actor = $gameParty.battleMembersNotPets()[index];
  if (!actor) return;
  let rect = this.itemRect(index);
  this.contents.clearRect(rect.x, rect.y, rect.width, rect.height);
  this._faceContents.bitmap.clearRect(rect.x, rect.y, rect.width, rect.height);//清除脸
  this.drawStatusFace(index);//补充画脸
  this._cursorContents.bitmap.clearRect(rect.x, rect.y, rect.width, rect.height);//清除光标
  this.drawCursor(index);
  this.drawItem(index);
  actor.completetStatusRefreshRequest();
};



SWDA_BattleStatusWindow.Window_BattleStatus_update = Window_BattleStatus.prototype.update;
Window_BattleStatus.prototype.update = function () {
  SWDA_BattleStatusWindow.Window_BattleStatus_update.call(this);
  this.drawAllCursors();
  this.updateOpacity();
};

Window_BattleStatus.prototype.updateOpacity = function () {
  var opacity = 0;
  if (this._opening||this._closing){
    opacity = this.openness;
  } else if (this.isOpen()){
    opacity = 255;
  } else if (this.isClosed()){
    opacity = 0;
  } else {
    opacity = this.openness;
  }
  this.contents.opacity = opacity;
  this._cursorContents.opacity = opacity;
  this._faceContents.opacity = opacity;
  this._atbContents.opacity = opacity;
};


Window_BattleStatus.prototype.updateOpen = function() {
  if (this._opening) {
      this.openness += 16;
      if (this.isOpen()) {
          this._opening = false;
      }
  }
};

Window_BattleStatus.prototype.updateClose = function() {
  if (this._closing) {
      this.openness -= 16;
      if (this.isClosed()) {
          this._closing = false;
      }
  }
};

Window_BattleStatus.prototype.maxRows = function () {
  let rows = 1;
  return rows;
};

Window_BattleStatus.prototype.maxCols = function () {
  if (Params.BSWAdjustCol) {
    return this.maxItems();
  } else {
    return $gameParty.maxBattleMembers();
  }
  return cols;
};

Window_BattleStatus.prototype.itemWidth = function () {
  return this.contents.width / this.maxCols();
};

Window_BattleStatus.prototype.spacing = function () {
  return 0;
};

Window_BattleStatus.prototype.itemHeight = function () {
  return this.lineHeight() * this.numVisibleRows();
};

Window_BattleStatus.prototype.drawItem = function (index) {
  //let actor = $gameParty.battleMembers()[index];//不显示宠物
  let actor = $gameParty.battleMembersNotPets()[index];
  this.drawBasicArea(this.basicAreaRect(index), actor);
  this.drawGaugeArea(this.gaugeAreaRect(index), actor);
  this.drawStateArea(this.stateAreaRect(index), actor);
};

//画ATB槽
Window_BattleStatus.prototype.drawATBArea = function (rect, actor) {
  if (Imported.YEP_X_BattleSysATB && Yanfly.Param.ATBGaugeStyle) {
    if (BattleManager.isATB()) {
      this.drawActorAtbGaugeSWD(actor, rect.x + 3, rect.y + rect.height - 11, rect.width - 18);
    }
  }
}
// 刷新ATB槽
Window_BattleStatus.prototype.redrawATB = function () {
  if (this.isATBGaugeStyle(0)) return;
  let rect;
  //for (let i = 0; i < $gameParty.battleMembers().length; ++i) {//不显示宠物
  for (let i = 0; i < $gameParty.battleMembersNotPets().length; ++i) {
    //let actor = $gameParty.battleMembers()[i];//不显示宠物
    let actor = $gameParty.battleMembersNotPets()[i];
    if (this.isATBGaugeStyle(1)) {
      rect = this.atbAreaRect(i);		//改独立区域
      this.contents.clearRect(rect.x + 3, rect.y + rect.height - 11, rect.width - 18);
      this.drawATBArea(rect, actor);	//改独立区域
    } else if (this.isATBGaugeStyle(2)) {
      this.redrawATBGaugeRect(i, actor);
    }
  }
};

Window_BattleStatus.prototype.drawBasicArea = function (rect, actor) {
  let iw = this.SWDFaceWidth();
  let ih = this.SWDFaceHeight();
  this.drawActorActionIcon(actor, rect.x + iw, rect.y + rect.height - ih + 4);
  this.setSmallerFont();
  this.contents.fontSize = 20;
  this.drawActorName(actor, rect.x + iw + 4, rect.y + rect.height - this.lineHeight() * 3 - 8, rect.width);
  this.resetFontSettings();
};
// 人物信息颜色
Window_BattleStatus.prototype.hpColor = function (actor) {
  if (actor.isDead()) {
    return this.deathColor();
  } else if (actor.isDying()) {
    return this.crisisColor();
  } else {
    return this.textColor(0);
  }
};

Window_BattleStatus.prototype.mpColor = function (actor) {
  return this.textColor(0);
};

Window_BattleStatus.prototype.tpColor = function (actor) {
  return this.textColor(0);
};




// 基本区域Rect
Window_BattleStatus.prototype.basicAreaRect = function (index) {
  let rect = this.itemRectForText(index);
  //rect.height = this.lineHeight() * 2;
  return rect;
};

// 状态区域Rect
Window_BattleStatus.prototype.stateAreaRect = function (index) {
  let rect = this.itemRectForText(index);
  rect.x = rect.x;
  rect.y = rect.height - this.SWDFaceHeight() + 5;
  rect.width = this.SWDFaceWidth();
  rect.height = this.SWDFaceHeight() - 8;
  return rect;
};

// 绘制状态
Window_BattleStatus.prototype.drawStateArea = function (rect, actor) {
  this.drawActorIcons(actor, rect.x, rect.y, rect.width, rect.height);
};


//独立ATB槽区域
Window_BattleStatus.prototype.atbAreaRect = function (index) {
  let rect = this.itemRectForText(index);
  rect.width = this.SWDFaceWidth();
  return rect;
};

Window_BattleStatus.prototype.drawGaugeArea = function (rect, actor) {
  this._enableYBuffer = true;
  let wy = rect.y + rect.height - this.lineHeight();
  let wymod = (Imported.YEP_CoreEngine) ? Yanfly.Param.GaugeHeight : 6;
  wymod = Math.max(16, wymod) * 1.5;
  this.drawActorHp(actor, rect.x, wy - wymod * 2, rect.width);
  if (this.getGaugesDrawn(actor) <= 2) {
    this.drawActorMp(actor, rect.x, wy - wymod, rect.width);
  } else {
    this.drawActorMp(actor, rect.x, wy - wymod, rect.width);
    this.drawActorTp(actor, rect.x, wy, rect.width);
  }
  this._enableYBuffer = false;
};




Window_BattleStatus.prototype.getGaugesDrawn = function (actor) {
  let value = 2;
  if ($dataSystem.optDisplayTp) value += 1;
  return value;
};

Window_BattleStatus.prototype.gaugeAreaRect = function (index) {
  let rect = this.itemRectForText(index);
  rect.x = rect.x + this.SWDFaceWidth();
  rect.width = 120;
  rect.height = this.contents.height - this.lineHeight() * 3;
  rect.y = this.contents.height - rect.height - 4;
  return rect;
};

// 【重要】画单人脸
Window_BattleStatus.prototype.drawStatusFace = function (index) {
  //let actor = $gameParty.battleMembers()[index];//不显示宠物
  let actor = $gameParty.battleMembersNotPets()[index];
  let rect = this.itemRect(index);
  let ww = this.SWDFaceWidth();
  let wh = this.SWDFaceHeight();
  let wx = rect.x;
  let wy = rect.y + rect.height - wh;
  if (actor.actorId() <= 4) {
    let faceName = "BattleFace" + actor.actorId() + ((actor.actorId()==1 && $gameSwitches.value(299)) ? "a" : "");
    this.drawFace(faceName, wx, wy, ww, wh, actor);
  }
};
//【重要】执行画脸
Window_BattleStatus.prototype.drawFace = function (fn, x, y, width, height, actor) {
  actor = actor || {};
  let bitmap = ImageManager.SWDA_loadSystemFace(fn);
  let sw = bitmap.width;
  let sh = this.SWDFaceHeight();
  let sy = 0;
  if (actor.isDying()) {
    sy = sh;
  } else if (actor.isDead()) {
    sy = sh * 2;
  } else {
    sy = 0;
  }
  this._faceContents.bitmap.blt(bitmap, 0, sy, sw, sh, x, y);

};

//【重要】画光标
Window_BattleStatus.prototype.drawCursor = function (index) {
  //let actor = $gameParty.battleMembers()[index];//不显示宠物
  let actor = $gameParty.battleMembersNotPets()[index];
  let rect = this.itemRect(index);
  let bitmap = ImageManager.SWDA_loadSystemFace('BattleFaceCursor');
  let sw = this.SWDFaceWidth();
  let sh = this.SWDFaceHeight() + 32;
  let x = rect.x;
  let y = rect.y + rect.height - sh;
  let sy = 0


  //目标选中
  if ((actor.isSelected())) {
    sy = sh * 2;
    //窗口选中
  } else if ((this._index == index)) {
    sy = sh;
    //未选择
  } else {
    sy = 0;
  }
  this._cursorContents.bitmap.blt(bitmap, 0, sy, sw, sh, x, y);
};

Window_BattleStatus.prototype.updateTransform = function () {
  Window_Selectable.prototype.updateTransform.call(this);
  this.updateFaceContents();
};

Window_BattleStatus.prototype.updateFaceContents = function () {
  let w = this._width - this._padding * 2;
  let h = this._height - this._padding * 2;

  if (w > 0 && h > 0) {
    this._faceContents.setFrame(this.origin.x, this.origin.y, w, h);
    this._faceContents.visible = true;
  } else {
    this._faceContents.visible = false;
  }

};

Window_BattleStatus.prototype.drawText = function (text, wx, wy, ww, align) {
  if (this._enableYBuffer) {
    wy += Params.BSWParamYBuffer;
    wx += 2;
    ww -= 4;
  }
  Window_Selectable.prototype.drawText.call(this, text, wx, wy, ww, align);
};

Window_BattleStatus.prototype.drawCurrentAndMax = function (current, max, x, y,
  width, color1, color2) {
  this.setSmallerFont();
  this.contents.fontSize = 18;
  if (this._currentMax) {
    Window_Selectable.prototype.drawCurrentAndMax.call(this, current, max,
      x, y - 4, width, color1, this.textColor(0));	// 修改最大值颜色
  } else {
    this.changeTextColor(color1);
    let value = SWDA_BattleStatusWindow.Util.toGroup(current);
    this.drawText(value, x, y, width, 'right');
  }
};

Window_BattleStatus.prototype.drawItemGaugeIcon = function (iconIndex, wx, wy) {
  let bitmap = ImageManager.loadSystem('IconSet');
  let pw = Window_Base._iconWidth;
  let ph = Window_Base._iconHeight;
  let sx = iconIndex % 16 * pw;
  let sy = Math.floor(iconIndex / 16) * ph;
  let iconWidth = (Imported.YEP_CoreEngine) ? Params.GaugeHeight : 32;
  let iconHeight = (Imported.YEP_CoreEngine) ? Params.GaugeHeight : 32;
  wy += Window_Base._iconHeight - iconHeight;
  this.contents.blt(bitmap, sx, sy, pw, ph, wx, wy, iconWidth, iconHeight);
  return iconWidth;
};

//=============================================================================
// Scene_Battle【战斗场景】
//=============================================================================
// 刷新窗口位置【重要】（不移动位置）
Scene_Battle.prototype.updateWindowPositionsATB = function () {
  let activeWin = null;
  if (this._partyCommandWindow.active) activeWin = this._partyCommandWindow;
  if (this._actorCommandWindow.active) activeWin = this._actorCommandWindow;
  if (activeWin === null) return;
};

SWDA_BattleStatusWindow.Scene_Battle_createPartyCommandWindow =
  Scene_Battle.prototype.createPartyCommandWindow;
Scene_Battle.prototype.createPartyCommandWindow = function () {
  SWDA_BattleStatusWindow.Scene_Battle_createPartyCommandWindow.call(this);
  let win = this._partyCommandWindow;
  if (Params.BSWLfRt) {
    win.setHandler('right', this.commandFight.bind(this));
  }
  if (Params.BSWPageUpDn) {
    win.setHandler('pagedown', this.commandFight.bind(this));
  };
};

SWDA_BattleStatusWindow.Scene_Battle_createActorCommandWindow =
  Scene_Battle.prototype.createActorCommandWindow;
Scene_Battle.prototype.createActorCommandWindow = function () {
  SWDA_BattleStatusWindow.Scene_Battle_createActorCommandWindow.call(this);
  let win = this._actorCommandWindow;
  if (Params.BSWLfRt) {
    win.setHandler('left', this.selectPreviousCommand.bind(this));
    win.setHandler('right', this.selectRightCommand.bind(this));
  }
  if (Params.BSWPageUpDn) {
    win.setHandler('pageup', this.selectPreviousCommand.bind(this));
    win.setHandler('pagedown', this.selectRightCommand.bind(this));
  };
};

Scene_Battle.prototype.clearInputtingAction = function () {
  let action = BattleManager.inputtingAction();
  if (action) action.clear();
};

SWDA_BattleStatusWindow.Scene_Battle_onActorCancel = Scene_Battle.prototype.onActorCancel;
Scene_Battle.prototype.onActorCancel = function () {
  SWDA_BattleStatusWindow.Scene_Battle_onActorCancel.call(this);
  this.clearInputtingAction();
};

SWDA_BattleStatusWindow.Scene_Battle_onEnemyCancel = Scene_Battle.prototype.onEnemyCancel;
Scene_Battle.prototype.onEnemyCancel = function () {
  SWDA_BattleStatusWindow.Scene_Battle_onEnemyCancel.call(this);
  this.clearInputtingAction();
};

SWDA_BattleStatusWindow.Scene_Battle_onSkillCancel = Scene_Battle.prototype.onSkillCancel;
Scene_Battle.prototype.onSkillCancel = function () {
  SWDA_BattleStatusWindow.Scene_Battle_onSkillCancel.call(this);
  this.clearInputtingAction();
};

SWDA_BattleStatusWindow.Scene_Battle_onItemCancel = Scene_Battle.prototype.onItemCancel;
Scene_Battle.prototype.onItemCancel = function () {
  SWDA_BattleStatusWindow.Scene_Battle_onItemCancel.call(this);
  this.clearInputtingAction();
};

Scene_Battle.prototype.selectRightCommand = function () {
  if (!this.isAllowRightCommand()) {
    return this._actorCommandWindow.activate();
  }
  if (Imported.YEP_BattleEngineCore && BattleManager.isTickBased()) {
    if (BattleManager.actor()) BattleManager.actor().onTurnStart();
  }
  this.selectNextCommand();
};

Scene_Battle.prototype.isAllowRightCommand = function () {
  if (Params.BSWTurnSkip) return true;
  if (Imported.YEP_BattleEngineCore && BattleManager.isTickBased()) {
    return false;
  }
  return true;
};

//=============================================================================
// Utilities
//=============================================================================

SWDA_BattleStatusWindow.Util = SWDA_BattleStatusWindow.Util || {};

if (!SWDA_BattleStatusWindow.Util.toGroup) {
  SWDA_BattleStatusWindow.Util.toGroup = function (inVal) {
    return inVal;
  }
};

